package com.fh.controller;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/11/2112:05
 */

import com.fh.common.yyqData;
import com.fh.entity.po.Org;
import com.fh.entity.po.Type;
import com.fh.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * created by macro on 2021/11/21,
 */
@RestController
@RequestMapping("type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("queryAllTypeData")
    private yyqData queryAllTypeData() {
        return yyqData.success(typeService.queryAllTypeData());
    }

    @PostMapping("deleteTypeById")
    private yyqData deleteTypeById(Integer id) {
        if (id == null) {
            return yyqData.error(400, "缺少参数");
        }
        typeService.deleteTypeById(id);
        this.reflushType();
        return yyqData.success("");
    }

    @PostMapping("addTypeData")
    private yyqData addTypeData(Type type) {
        if (type.getPid() == null) {
            return yyqData.error(400, "缺少参数");
        }
        if (StringUtils.isEmpty(type.getName())) {
            return yyqData.error(400, "缺少参数");
        }
        typeService.addTypeData(type);
        this.reflushType();
        return yyqData.success("");
    }

    @GetMapping("queryById")
    private yyqData queryById(Integer id) {
        if (id == null) {
            return yyqData.error(400, "缺少参数");
        }
        return yyqData.success(typeService.queryById(id));
    }


    @PostMapping("updateType")
    private yyqData updateType(Type type) {
        if (type.getId() == null) {
            return yyqData.error(400, "缺少参数");
        }
        if (StringUtils.isEmpty(type.getName())) {
            return yyqData.error(400, "缺少参数");
        }
        typeService.updateType(type);
        this.reflushType();
        return yyqData.success("");
    }

    @RequestMapping("queryChildrenStr")
    public yyqData queryChildrenStr() {
        return yyqData.success(typeService.queryAllDataStr());
    }



    @GetMapping("reflushType")
    public yyqData reflushType(){
        typeService.reflushType();
        return yyqData.success("");
    }




}
